package com.example.demo.service.impl;

import com.example.demo.entity.*;
import com.example.demo.mapper.FoodMapper;
import com.example.demo.service.FoodService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class FoodServiceImpl implements FoodService {
    @Autowired
    private FoodMapper foodMapper;
    @Override
    public List<FoodTypeInfo> findFoodTypeInfo() {
        return foodMapper.findFoodTypeInfo();
    }

    @Override
    public List<FoodAreaInfo> findFoodAreaInfo() {
        return foodMapper.findFoodAreaInfo();
    }

    @Override
    public List<FoodInfo> findFoodInfo() {
        return foodMapper.findFoodInfo();
    }

    @Override
    public List<FoodPointRank> findFoodPointRank() {
        return foodMapper.findFoodPointRank();
    }

    @Override
    public List<FoodPopularityRank> findFoodPopularityRank() {
        return foodMapper.findFoodPopularityRank();
    }

    @Override
    public PageInfo<FoodInfo> selectFoodPage(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<FoodInfo> foodInfos=foodMapper.findFoodInfo();
        PageInfo<FoodInfo> foodInfoPageInfo =new PageInfo<FoodInfo>(foodInfos);


        return foodInfoPageInfo;
    }

    @Override
    public FoodInfo findFoodInfoByname(String name) {
        return foodMapper.findFoodInfoByname(name);
    }

    @Override
    public void insertFood(FoodInfo foodInfo) {
        foodMapper.insertFood(foodInfo);
        foodMapper.addFoodAreaInfo(foodInfo.getArea());
        foodMapper.addFoodTypeInfo(foodInfo.getType());
        foodMapper.insertFoodPointRank(foodInfo);
        foodMapper.insertFoodPopularity(foodInfo);
    }

    @Override
    public FoodInfo findFoodById(int id) {
        return foodMapper.findFoodById(id);
    }

    @Override
    public void updateFood(FoodInfo foodInfo) {
        FoodInfo foodInfodb=foodMapper.findFoodById(foodInfo.getId());
        foodMapper.updateFood(foodInfo);
        foodMapper.addFoodAreaInfo(foodInfo.getArea());
        foodMapper.addFoodTypeInfo(foodInfo.getType());
        FoodPointRank foodPointRank=foodMapper.findFoodPointRankByname(foodInfodb.getName());
        FoodPopularityRank foodPopularityRank=foodMapper.findFoodPopularityRankByname(foodInfodb.getName());
        if (foodPointRank!=null){
            foodInfo.setId(foodPointRank.getId());
            foodMapper.updateFoodPointRank(foodInfo);

        }
        if (foodPopularityRank!=null){

            foodInfo.setId(foodPopularityRank.getId());
            foodMapper.updateFoodPopularityRank(foodInfo);
        }


    }

    @Override
    public void updateFoodEX(FoodInfo foodInfo) {
        foodMapper.reduceFoodAreaInfo(foodInfo.getArea());
        foodMapper.reduceFoodTypeInfo(foodInfo.getType());
    }

    @Override
    public void deleteFood(int id) {
        FoodInfo foodInfo=foodMapper.findFoodById(id);
        foodMapper.deleteFoodById(id);
        foodMapper.reduceFoodAreaInfo(foodInfo.getArea());
        foodMapper.reduceFoodTypeInfo(foodInfo.getType());
        foodMapper.deleteFoodPointRankByName(foodInfo.getName());
        foodMapper.deleteFoodPopularRankByName(foodInfo.getName());
    }
}

